home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PC World Komputer 2010 April
/
PCWorld0410.iso
/
pluginy Firefox
/
6584
/
6584.xpi
/
chrome
/
flashvideodownloader
/
content
/
download_frm_page.js
< prev
next >
Wrap
Text File
|
2009-11-26
|
13KB
|
415 lines
var FlashVDFrm = {
options : null,
rootUrl : null,
urls : null,
nodeByUrl : null,
serviceNeed : null,
elementsByUrl : {},
init : function()
{
this.options = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService).getBranch("FlashVD.options.");
this.rootUrl = window.arguments[0];
this.urls = window.arguments[1];
this.nodeByUrl = window.arguments[2];
this.serviceNeed = window.arguments[3];
var folder = false;
try { folder = this.options.getComplexValue ("favorite-folder", Components.interfaces.nsISupportsString); } catch(e) {}
if(folder){window.document.getElementById('fav_folder_text').value=folder;}
// add "Downliad All" button
if (this.urls.length > 1)
{
var bbox=document.createElement("box");
var bbutt=document.createElement("button");
var sitename=document.createElement("description");
var countInfo=document.createElement("description");
bbutt.setAttribute("onclick","FlashVDFrm.download_all();");
bbutt.setAttribute("style","text-align:center;font-size:100%;font-weight:bold;");
bbutt.setAttribute("label","Download All ");
bbutt.setAttribute("image","chrome://flashvideodownloader/skin/16x16.png");
bbutt.setAttribute("dir","reverse");
//add caption siteURL
sitename.appendChild(document.createTextNode(this.rootUrl + "..."));
sitename.setAttribute("style","padding-top:4px;");
//add count info
countInfo.appendChild(document.createTextNode("(" + this.urls.length + " item(s))"));
countInfo.setAttribute("style","padding-top:4px;font-weight:bold;");
//sobiraem vse vmeste i dobavlyaem v okno
bbox.appendChild(bbutt);
bbox.appendChild(sitename);
bbox.appendChild(countInfo);
bbox.setAttribute("style","padding:3px 0;");
document.getElementById('sites').appendChild(bbox);
}
for(var i = 0; i < this.urls.length; i++)
{
var url=this.urls[i];
var bbox=document.createElement("box");
var bbutt=document.createElement("button");
var sitename=document.createElement("description");
bbutt.setAttribute("onclick","FlashVDFrm.download_open(this);");
bbutt.setAttribute("style","text-align:center;font-size:100%;");
bbutt.setAttribute("onmouseover","FlashVDFrm.button_onmouseover(this);");
bbutt.setAttribute("onmouseout","FlashVDFrm.button_onmouseout(this);");
//bbutt.appendChild(document.createTextNode("download"));
bbutt.setAttribute("label","Download ");
bbutt.setAttribute("image","chrome://flashvideodownloader/skin/16x16.png");
bbutt.setAttribute("dir","reverse");
//delaem i dobavlyaem iconku
//var icon=document.createElement("image");
//icon.setAttribute("src","chrome://flashvideodownloader/skin/16x16.png");
//icon.setAttribute("style","padding:2px 0 0 4px;width:16px;height:16px;");
//bbutt.appendChild(icon);
//delaem nadpis siteURL
sitename.appendChild(document.createTextNode(url));
sitename.setAttribute("onmouseover","FlashVDFrm.label_onmouseover(this);");
sitename.setAttribute("onmouseout","FlashVDFrm.label_onmouseout(this);");
sitename.setAttribute("style","padding-top:4px;");
//sobiraem vse vmeste i dobavlyaem v okno
bbox.appendChild(bbutt);
bbox.appendChild(sitename);
bbox.setAttribute("style","padding:3px 0;");
document.getElementById('sites').appendChild(bbox);
var elemsItem = { button : null, label : null };
elemsItem.button = bbutt;
elemsItem.label = sitename;
this.elementsByUrl[url] = elemsItem;
}
},
getfilename : function (s)
{
var filename = s;
var p = filename.lastIndexOf("/");
if (p >= 0)
{
filename = filename.substring(p+1, filename.length);
}
return filename;
},
button_onmouseover : function (elem)
{
try
{
var surl=elem.parentNode.lastChild.lastChild.data;
var node = this.nodeByUrl[surl];
this.onmouseover(node);
}
catch(e)
{
aler("over: " + e);
}
},
button_onmouseout : function (elem)
{
try
{
var surl=elem.parentNode.lastChild.lastChild.data;
var node = this.nodeByUrl[surl];
this.onmouseout(node);
}
catch(e)
{
aler("out: " + e);
}
},
label_onmouseover : function (elem)
{
try
{
var surl=elem.lastChild.data;
var node = this.nodeByUrl[surl];
this.onmouseover(node);
}
catch(e)
{
aler("over: " + e);
}
},
label_onmouseout : function (elem)
{
try
{
var surl=elem.lastChild.data;
var node = this.nodeByUrl[surl];
this.onmouseout(node);
}
catch(e)
{
aler("out: " + e);
}
},
onmouseover : function (node)
{
try
{
var oldBorder=node.style.border;
node.setAttribute("fvd-border",oldBorder);
node.style.border="3px dotted Chartreuse";
var oldDisplay=node.style.display;
node.setAttribute("fvd-display",oldDisplay);
node.style.display="block";
}
catch(e) {}
},
onmouseout : function (node)
{
try
{
var oldBorder=node.getAttribute("fvd-border");
node.style.border=oldBorder;
var oldDisplay=node.getAttribute("fvd-display");
node.style.display=oldDisplay;
}
catch(e) {}
},
download_open : function (elem)
{
var url=elem.parentNode.lastChild.lastChild.data;
this.download_url(url);
},
download_all : function ()
{
for(var i = 0; i < this.urls.length; i++)
{
var url=this.urls[i];
this.download_url(url);
}
},
download_url : function (url)
{
if (this.serviceNeed[url] == true)
{
// replaced by opening window in new tab
//window.open('http://www.flashvideodownloader.org/download2.php?u='+url,"","width=500,height=500,resizable=no");
// get the browser object
var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"]
.getService(Components.interfaces.nsIWindowMediator);
var mainWindow = wm.getMostRecentWindow("navigator:browser");
var browser = mainWindow.getBrowser();
// add new tab with our url
var tab = browser.addTab('http://www.flashvideodownloader.org/download2.php?u='+url);
// make the tab active
browser.selectedTab = tab;
return;
}
try
{
var folder = this.get_folder_internal();
if (!folder)
{
return;
}
// try getDefaultFileName
var filename = this.getfilename(url);
var uri = Components.classes["@mozilla.org/network/io-service;1"]
.getService(Components.interfaces.nsIIOService)
.newURI(url, null, null);
//var hrefParts=/^(.*[^0-9])?([0-9]+)([^\/]*?\.[^\/]*?)$/.exec(url);
//var ext = /.*\.(.*?)$/.exec(hrefParts[3])[1];
//var filename=/^(.*?)(?:\.[^\.]{1,5})?$/.exec(filename)[1]+"."+ext;
var file = Components.classes["@mozilla.org/file/local;1"]
.createInstance(Components.interfaces.nsILocalFile);
file.initWithPath(folder + "\\" + filename); // download destination
var fileUri = Components.classes["@mozilla.org/network/io-service;1"]
.getService(Components.interfaces.nsIIOService)
.newFileURI(file);
// create a persist
var persist = Components.classes["@mozilla.org/embedding/browser/nsWebBrowserPersist;1"].
createInstance(Components.interfaces.nsIWebBrowserPersist);
// with persist flags if desired See nsIWebBrowserPersist page for more PERSIST_FLAGS.
const flags = persist.PERSIST_FLAGS_REPLACE_EXISTING_FILES;
persist.persistFlags = flags | persist.PERSIST_FLAGS_FROM_CACHE;
// transfer - these 3 lines of code shows standard FF Download Manager
//var transfer = Components.classes["@mozilla.org/transfer;1"].createInstance(Components.interfaces.nsITransfer);
//transfer.init(uri, fileUri, "", null, null, null, persist);
//persist.progressListener = transfer;
// transfer - these 3 lines of code shows standard FF Download Manager
// + we want to know when donloading finished, so we create self progress listener and wrap the standard one by him
// owr progressListener call this.transferDone(url) when finished.
var transfer = Components.classes["@mozilla.org/transfer;1"].createInstance(Components.interfaces.nsITransfer);
transfer.init(uri, fileUri, "", null, null, null, persist);
var progress = new Progress(transfer, this, url);
persist.progressListener = progress;
this.setDownloadingStyle(url, "start downloading");
persist.saveURI(uri, null, null, null, "", file);
}
catch(e)
{
alert("There was an exception.\n\n'" + e + "\n\n" + url + "' download terminated.");
}
},
setDownloadingStyle : function(url, state)
{
// 1. Find elements
//alert("setDownloadingStyle, url=" + url + ", state=" + state);
var elemsItem = this.elementsByUrl[url];
// 2. Set elements style
// TODO: use somthing like enums instead simple strings
if (elemsItem == null)
return;
if (state == "start downloading")
{
elemsItem.button.setAttribute("label","Downloading... ");
elemsItem.button.setAttribute("image","chrome://flashvideodownloader/skin/16x16.png");
elemsItem.label.setAttribute("style","padding-top:4px;color:gray;");
}
else if (state == "downloaded")
{
elemsItem.button.setAttribute("label","Downloaded ");
elemsItem.button.setAttribute("image","chrome://flashvideodownloader/skin/tick_16.png");
elemsItem.label.setAttribute("style","padding-top:4px;");
}
else //if (state == "any state")
{
elemsItem.button.setAttribute("label","Download ");
elemsItem.button.setAttribute("image","chrome://flashvideodownloader/skin/16x16.png");
elemsItem.label.setAttribute("style","padding-top:4px;");
}
},
transferDone : function(status, request, url)
{
//alert("transferDone, url=" + url);
var code=0;
try
{
var hc = request.QueryInterface(Components.interfaces.nsIHttpChannel);
code = hc.responseStatus;
} catch(e) {}
if (status==0 && code==200)
{
this.setDownloadingStyle(url, "downloaded");
}
else
{
this.setDownloadingStyle(url, "err");
}
},
get_folder_internal : function() {
var folder = null;
try { folder = this.options.getComplexValue ("favorite-folder", Components.interfaces.nsISupportsString); } catch(e) {}
if (folder == null || folder == "")
folder = this.get_folder();
return folder;
},
get_folder : function() {
var folder = this.onPickFolder();
if (folder)
this.set_folder(folder);
return folder;
},
set_folder : function(folder) {
var s = Components.classes["@mozilla.org/supports-string;1"].createInstance(Components.interfaces.nsISupportsString);
s.data = folder;
this.options.setComplexValue("favorite-folder", Components.interfaces.nsISupportsString, s);
window.document.getElementById('fav_folder_text').value=folder;
},
onPickFolder : function() {
var fp = Components.classes["@mozilla.org/filepicker;1"].createInstance(Components.interfaces.nsIFilePicker);
fp.init(window,'Choose folder for videos and games:', Components.interfaces.nsIFilePicker.modeGetFolder);
var res = fp.show();
if (res == Components.interfaces.nsIFilePicker.returnOK)
return fp.file.path;
return false;
}
}
function Progress(transfer,observer,url)
{
this.transfer=transfer;
this.observer=observer;
this.url=url;
//alert("Progress: url=" + url + ", this.url=" + this.url);
}
Progress.prototype.onLocationChange=function(webProgress, request, location )
{
this.transfer.onLocationChange(webProgress, request, location);
}
Progress.prototype.onProgressChange=function(webProgress, request, curSelfProgress, maxSelfProgress, curTotalProgress, maxTotalProgress )
{
try
{
this.transfer.onProgressChange(webProgress, request, curSelfProgress, maxSelfProgress, curTotalProgress, maxTotalProgress );
}
catch(e) {}
}
Progress.prototype.onSecurityChange=function(webProgress, request, state )
{
this.transfer.onSecurityChange(webProgress, request, state );
}
Progress.prototype.onStateChange=function(webProgress, request, stateFlags, status )
{
this.transfer.onStateChange(webProgress, request, stateFlags, status );
if(stateFlags & Components.interfaces.nsIWebProgressListener.STATE_STOP)
{
//alert("Progress.prototype.onStateChange: this.url=" + this.url);
this.observer.transferDone(status,request,this.url);
}
}
Progress.prototype.onStatusChange=function(webProgress, request, status, message )
{
this.transfer.onStatusChange(webProgress, request, status, message );
}